<?php

namespace app\models\order;

use app\models\OrderActive;
use app\models\product\Product;
use Yii;

/**
 * This is the model class for table "order_item".
 *
 * @property int $id
 * @property int $order_id
 * @property int $product_id
 * @property int $product_sku_id
 * @property string $selected_sku_value sku json {id}
 * @property string $selected_sku_text sku json {text}
 * @property int $quantity 数量
 * @property float $price 价格
 * @property float $freight 运费
 * @property float $score 积分
 * @property int $is_express 是否需要发货 1 需要 0 不需要
 */
class OrderItem extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'order_item';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['order_id', 'product_id', 'product_sku_id', 'quantity'], 'integer'],
            [['selected_sku_value', 'selected_sku_text'], 'safe'],
            [['price', 'freight', 'score'], 'number'],

            [['is_express'], 'safe'],

        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'order_id' => 'Order ID',
            'product_id' => 'Product ID',
            'product_sku_id' => 'Product Sku ID',
            'selected_sku_value' => 'Selected Sku Value',
            'selected_sku_text' => 'Selected Sku Text',
            'quantity' => 'Quantity',
            'price' => 'Price',
            'freight' => 'Freight',
        ];
    }

    public function getProduct()
    {
        return $this->hasOne(Product::class, ['id' => 'product_id']);
    }

    public function getLogistics()
    {
        return $this->hasOne(OrderLogistics::class, ['order_item_id' => 'id', 'order_id' => 'order_id'])
            ->orderBy(['id' => SORT_DESC]);
    }

    // 激活码
    public function getActive0()
    {
        return $this->hasMany(OrderActive::class, ['order_item_id' => 'id']);
    }
}
